<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/myblog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/myblog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/myblog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/myblog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/myblog/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"kuang-yudi.gitee.io","root":"/myblog/","images":"/myblog/images","scheme":"Gemini","version":"8.1.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"gitalk","storage":true,"lazyload":false,"nav":null,"activeClass":"gitalk"},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/myblog/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="js中的继承方法">
<meta property="og:type" content="article">
<meta property="og:title" content="js中的继承方法">
<meta property="og:url" content="http://kuang-yudi.gitee.io/myblog/2021/01/13/js%E4%B8%AD%E7%9A%84%E7%BB%A7%E6%89%BF%E6%96%B9%E6%B3%95/index.html">
<meta property="og:site_name" content="KY迪的个人博客">
<meta property="og:description" content="js中的继承方法">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-01-13T11:06:38.000Z">
<meta property="article:modified_time" content="2021-01-18T06:33:26.700Z">
<meta property="article:author" content="匡宇迪">
<meta property="article:tag" content="js">
<meta property="article:tag" content="继承">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://kuang-yudi.gitee.io/myblog/2021/01/13/js%E4%B8%AD%E7%9A%84%E7%BB%A7%E6%89%BF%E6%96%B9%E6%B3%95/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>js中的继承方法 | KY迪的个人博客</title>
  



  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/myblog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">KY迪的个人博客</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">个人技术博客</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/myblog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-tags"><a href="/myblog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-categories"><a href="/myblog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-archives"><a href="/myblog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <section class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8E%9F%E5%9E%8B%E9%93%BE%E7%BB%A7%E6%89%BF"><span class="nav-number">1.</span> <span class="nav-text">原型链继承</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E9%87%8D%E7%82%B9%EF%BC%9A%E8%AE%A9%E5%AD%90%E7%B1%BB%E7%9A%84%E5%8E%9F%E5%9E%8B%E5%AF%B9%E8%B1%A1%E7%AD%89%E4%BA%8E%E7%88%B6%E7%B1%BB%E7%9A%84%E5%AE%9E%E4%BE%8B-%E5%B0%86%E7%88%B6%E7%B1%BB%E6%94%BE%E5%9C%A8%E5%AD%90%E7%B1%BB%E7%9A%84%E5%8E%9F%E5%9E%8B%E9%93%BE%E4%B8%8A-%E3%80%82"><span class="nav-number">1.1.</span> <span class="nav-text">1. 重点：让子类的原型对象等于父类的实例(将父类放在子类的原型链上)。</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%89%B9%E7%82%B9%EF%BC%9A"><span class="nav-number">1.2.</span> <span class="nav-text">2. 特点：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E7%BC%BA%E7%82%B9%EF%BC%9A"><span class="nav-number">1.3.</span> <span class="nav-text">3. 缺点：</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%9E%84%E9%80%A0%E7%BB%A7%E6%89%BF-call-%E7%BB%A7%E6%89%BF"><span class="nav-number">2.</span> <span class="nav-text">构造继承(call 继承)</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%A0%B8%E5%BF%83%EF%BC%9A%E5%9C%A8%E5%AD%90%E7%B1%BB%E4%B8%AD%E8%B0%83%E7%94%A8%E7%88%B6%E7%B1%BB%E5%B9%B6%E9%80%9A%E8%BF%87-call-%E6%96%B9%E6%B3%95%E5%B0%86%E7%88%B6%E7%B1%BB%E4%B8%AD%E7%9A%84-this-%E6%8C%87%E5%90%91%E5%AD%90%E7%B1%BB%E5%AE%9E%E4%BE%8B-%E7%BB%A7%E6%89%BF%E4%BA%86%E7%88%B6%E7%B1%BB%E7%9A%84%E5%B1%9E%E6%80%A7%EF%BC%8C%E4%B8%8D%E7%BB%A7%E6%89%BF%E7%88%B6%E7%B1%BB%E7%9A%84%E5%85%AC%E6%9C%89%E6%96%B9%E6%B3%95%E5%8D%B3%E5%8E%9F%E5%9E%8B%E5%AF%B9%E8%B1%A1%E3%80%82"><span class="nav-number">2.1.</span> <span class="nav-text">1. 核心：在子类中调用父类并通过 call 方法将父类中的 this 指向子类实例(继承了父类的属性，不继承父类的公有方法即原型对象。)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%89%B9%E7%82%B9%EF%BC%9A-1"><span class="nav-number">2.2.</span> <span class="nav-text">2. 特点：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E7%BC%BA%E7%82%B9%EF%BC%9A-1"><span class="nav-number">2.3.</span> <span class="nav-text">3. 缺点：</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%AF%84%E7%94%9F%E7%BB%84%E5%90%88%E7%BB%A7%E6%89%BF-%E6%8E%A8%E8%8D%90"><span class="nav-number">3.</span> <span class="nav-text">寄生组合继承(推荐)</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%A0%B8%E5%BF%83"><span class="nav-number">3.1.</span> <span class="nav-text">1. 核心</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%89%B9%E7%82%B9"><span class="nav-number">3.2.</span> <span class="nav-text">2. 特点</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#ES6-%E4%B8%AD%E7%9A%84%E7%BB%A7%E6%89%BF%E2%80%94%E2%80%94extends"><span class="nav-number">4.</span> <span class="nav-text">ES6 中的继承——extends</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%89%8D%E6%8F%90%E6%B3%A8%E6%84%8F"><span class="nav-number">4.1.</span> <span class="nav-text">前提注意</span></a></li></ol></li></ol></div>
        </section>
        <!--/noindex-->

        <section class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="匡宇迪"
      src="https://static.pptstore.net/picture/19/19/e1feb44f42cdf7e1202b_s.jpg">
  <p class="site-author-name" itemprop="name">匡宇迪</p>
  <div class="site-description" itemprop="description">用于记录学习web前端</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/myblog/archives/">
        
          <span class="site-state-item-count">38</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/myblog/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/myblog/tags/">
          
        <span class="site-state-item-count">87</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/tonykyd/tonykyd.github.io" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tonykyd&#x2F;tonykyd.github.io" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1303665509@qq.com" title="E-Mail → mailto:1303665509@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



        </section>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://kuang-yudi.gitee.io/myblog/2021/01/13/js%E4%B8%AD%E7%9A%84%E7%BB%A7%E6%89%BF%E6%96%B9%E6%B3%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://static.pptstore.net/picture/19/19/e1feb44f42cdf7e1202b_s.jpg">
      <meta itemprop="name" content="匡宇迪">
      <meta itemprop="description" content="用于记录学习web前端">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="KY迪的个人博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          js中的继承方法
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-01-13 19:06:38" itemprop="dateCreated datePublished" datetime="2021-01-13T19:06:38+08:00">2021-01-13</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-01-18 14:33:26" itemprop="dateModified" datetime="2021-01-18T14:33:26+08:00">2021-01-18</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/myblog/categories/%E9%9D%A2%E8%AF%95%E9%A2%98/" itemprop="url" rel="index"><span itemprop="name">面试题</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

            <div class="post-description">js中的继承方法</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="原型链继承"><a href="#原型链继承" class="headerlink" title="原型链继承"></a>原型链继承</h1><h2 id="1-重点：让子类的原型对象等于父类的实例-将父类放在子类的原型链上-。"><a href="#1-重点：让子类的原型对象等于父类的实例-将父类放在子类的原型链上-。" class="headerlink" title="1. 重点：让子类的原型对象等于父类的实例(将父类放在子类的原型链上)。"></a>1. 重点：让子类的原型对象等于父类的实例(将父类放在子类的原型链上)。</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;构造函数——父类</span><br><span class="line">function Animal(name) &#123;</span><br><span class="line">	this.name &#x3D; name || &#39;Animal&#39;;</span><br><span class="line">	this.sleep &#x3D; function() &#123;</span><br><span class="line">		console.log(this.name + &#39;正在睡觉！&#39;);</span><br><span class="line">	&#125;;</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F;原型上面的方法：</span><br><span class="line">Animal.prototype.eat &#x3D; function(food) &#123;</span><br><span class="line">	console.log(this.name + &#39;正在吃:&#39; + food);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;核心：将父类的实例作为子类的原型</span><br><span class="line">&#x2F;&#x2F;子类</span><br><span class="line">function Dog() &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">Dog.prototype &#x3D; new Animal();  &#x2F;&#x2F;将Animal的实例挂载到了Dog的原型链上</span><br><span class="line">&#x2F;&#x2F; 保证子类Dog的原型重定向后的完整性</span><br><span class="line">Dog.prototype.constructor&#x3D;Dog;</span><br><span class="line">&#x2F;&#x2F;或：Object.create(Animal.prototype):创建一个空对象且空对象的原型__proto__指向父类原型对象</span><br><span class="line">&#x2F;&#x2F;Dog.prototype &#x3D; Object.create(Animal.prototype)</span><br><span class="line">Dog.prototype.name &#x3D; &#39;dog&#39;;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;子类的实例对象</span><br><span class="line">var dog &#x3D; new Dog();</span><br><span class="line">console.log(dog.name);		&#x2F;&#x2F;dog</span><br><span class="line">dog.eat(&#39;bone&#39;);		&#x2F;&#x2F;dog正在吃:bone</span><br><span class="line">dog.sleep();		&#x2F;&#x2F;dog正在睡觉！</span><br><span class="line">console.log(dog instanceof Animal);		&#x2F;&#x2F;true</span><br><span class="line">console.log(dog instanceof Dog);		&#x2F;&#x2F;true</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="2-特点："><a href="#2-特点：" class="headerlink" title="2. 特点："></a>2. 特点：</h2><ol>
<li>非常纯粹的继承关系，实例既是子类的实例，也是父类的实例</li>
<li>父类新增原型方法/原型属性，子类都能访问的到</li>
</ol>
<h2 id="3-缺点："><a href="#3-缺点：" class="headerlink" title="3. 缺点："></a>3. 缺点：</h2><ol>
<li>要想为子类新增属性和方法，必须要在 new Animal()这样的语句之后执行，不能放到构造器中</li>
<li>无法实现继承多个</li>
<li>来自原型对象的所有属性被所有实例共享(父类中公有属性方法都成为子类中公有的方法)</li>
<li>创建子类实例时，无法向父类构造函数传参</li>
</ol>
<hr>
<h1 id="构造继承-call-继承"><a href="#构造继承-call-继承" class="headerlink" title="构造继承(call 继承)"></a>构造继承(call 继承)</h1><h2 id="1-核心：在子类中调用父类并通过-call-方法将父类中的-this-指向子类实例-继承了父类的属性，不继承父类的公有方法即原型对象。"><a href="#1-核心：在子类中调用父类并通过-call-方法将父类中的-this-指向子类实例-继承了父类的属性，不继承父类的公有方法即原型对象。" class="headerlink" title="1. 核心：在子类中调用父类并通过 call 方法将父类中的 this 指向子类实例(继承了父类的属性，不继承父类的公有方法即原型对象。)"></a>1. 核心：在子类中调用父类并通过 call 方法将父类中的 this 指向子类实例(继承了父类的属性，不继承父类的公有方法即原型对象。)</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;核心：使用父类的构造函数增强子类实例，等于是复制父类的实例属性给子类（没用到原型）</span><br><span class="line">function Cat(name) &#123;</span><br><span class="line">  &#x2F;&#x2F; 让父类Animal中的this指向子类Cat的实例</span><br><span class="line">	Animal.call(this);</span><br><span class="line">	this.name &#x3D; name || &#39;Tom&#39;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">var cat &#x3D; new Cat();</span><br><span class="line">console.log(cat.name);		&#x2F;&#x2F;Tom</span><br><span class="line">cat.sleep();		&#x2F;&#x2F;Tom正在睡觉！</span><br><span class="line">console.log(cat instanceof Animal);		&#x2F;&#x2F;false</span><br><span class="line">console.log(cat instanceof Cat);		&#x2F;&#x2F;true</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="2-特点：-1"><a href="#2-特点：-1" class="headerlink" title="2. 特点："></a>2. 特点：</h2><ol>
<li>创建子类实例时，可以向父类传递参数</li>
<li>可以实现多继承（call 多个父类对象）</li>
</ol>
<h2 id="3-缺点：-1"><a href="#3-缺点：-1" class="headerlink" title="3. 缺点："></a>3. 缺点：</h2><ol>
<li>实例并不是父类的实例，只是子类的实例</li>
<li>只能继承父类的实例属性，不能继承原型对象中的方法</li>
<li>无法实现函数复用，每个子类都有父类实例函数的副本，影响性能</li>
</ol>
<hr>
<h1 id="寄生组合继承-推荐"><a href="#寄生组合继承-推荐" class="headerlink" title="寄生组合继承(推荐)"></a>寄生组合继承(推荐)</h1><h2 id="1-核心"><a href="#1-核心" class="headerlink" title="1. 核心"></a>1. 核心</h2><p>结合原型链继承和构造继承，注意在使用原型链继承时 Object.create()方法创建一个新对象，使用现有的对象作为参数来提供新创建的对象的__proto__原型</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;  父类</span><br><span class="line">      function Person(name,age)&#123;</span><br><span class="line">      	this.name &#x3D; name;</span><br><span class="line">      	this.age &#x3D; age;</span><br><span class="line">      &#125;</span><br><span class="line">      &#x2F;&#x2F; 父类的原型对象属性</span><br><span class="line">      Person.prototype.id &#x3D; 10;</span><br><span class="line"></span><br><span class="line">      &#x2F;&#x2F; 子类</span><br><span class="line">      function Boy(name,age,sex)&#123;</span><br><span class="line">      	&#x2F;&#x2F;call或apply 实现继承</span><br><span class="line">      	Person.call(this,name,age);</span><br><span class="line">      	this.sex &#x3D; sex;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      &#x2F;&#x2F; 原型继承实现 Object.create(Animal.prototype):创建一个新对象，使用现有的对象来提供新创建的对象的__proto__</span><br><span class="line">     Boy.prototype &#x3D; Object.create(Person.prototype)</span><br><span class="line">      &#x2F;&#x2F; 覆盖子类的prototype后保证子类中prototype的完整性</span><br><span class="line">      Boy.prototype.constructor&#x3D;Boy;</span><br><span class="line">      var b &#x3D; new Boy(&#39;c5&#39;,27,&#39;男&#39;);</span><br><span class="line">      alert(b.name)&#x2F;&#x2F; c5</span><br><span class="line">      alert(b.id)&#x2F;&#x2F;10</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="2-特点"><a href="#2-特点" class="headerlink" title="2. 特点"></a>2. 特点</h2><p>父类的公有属性、方法分别是子类的公有属性、方法</p>
<hr>
<h1 id="ES6-中的继承——extends"><a href="#ES6-中的继承——extends" class="headerlink" title="ES6 中的继承——extends"></a>ES6 中的继承——extends</h1><h2 id="前提注意"><a href="#前提注意" class="headerlink" title="前提注意"></a>前提注意</h2><p>ES6 中通过 class 声明类，此时不能通过直接单独写一个类名调用，也不允许重定向原型对象</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 构造方法（父类）</span><br><span class="line">class Phone &#123;</span><br><span class="line">  constructor(brand, price) &#123;</span><br><span class="line">    this.brand &#x3D; brand;</span><br><span class="line">    this.price &#x3D; price;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  &#x2F;&#x2F; 父类的成员属性</span><br><span class="line">  call() &#123;</span><br><span class="line">    console.log(&quot;我可以打电话&quot;);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 子类 extends继承</span><br><span class="line">class SmartPhone extends Phone &#123;</span><br><span class="line">  &#x2F;&#x2F; 构造方法</span><br><span class="line">  constructor(brand, price, color, size) &#123;</span><br><span class="line">    &#x2F;&#x2F; 构造方法中第一句super，实现call继承的功能(让父类中的this指向子类实例)</span><br><span class="line">    super(brand, price);</span><br><span class="line"></span><br><span class="line">    this.color &#x3D; color;</span><br><span class="line">    this.size &#x3D; size;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  photo() &#123;</span><br><span class="line">    console.log(&quot;拍照&quot;);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  playGame() &#123;</span><br><span class="line">    console.log(&quot;玩游戏&quot;);</span><br><span class="line">  &#125;</span><br><span class="line">  &#x2F;&#x2F; 对父类方法进行重写</span><br><span class="line">  &#x2F;&#x2F; call()&#123;</span><br><span class="line">  &#x2F;&#x2F;   console.log(&quot;我可以打视频电话&quot;);</span><br><span class="line">  &#x2F;&#x2F; &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 子类实例化</span><br><span class="line">const xiaomi &#x3D; new SmartPhone(&#39;小米&#39;, 1999, &#39;红色&#39;, &#39;4.7inch&#39;);</span><br><span class="line">console.log(xiaomi);</span><br><span class="line">xiaomi.call() &#x2F;&#x2F;调用父类中的call方法</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="followme">
  <span>欢迎关注我的其它发布渠道</span>

  <div class="social-list">

      <div class="social-item">
        <a target="_blank" class="social-link" href="/myblog/images/wechat_channel.jpg">
          <span class="icon">
            <i class="fab fa-weixin"></i>
          </span>

          <span class="label">WeChat</span>
        </a>
      </div>
  </div>
</div>

          <div class="post-tags">
              <a href="/myblog/tags/js/" rel="tag"># js</a>
              <a href="/myblog/tags/%E7%BB%A7%E6%89%BF/" rel="tag"># 继承</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/myblog/2021/01/13/js%E4%B8%AD%E6%95%B0%E7%BB%84%E3%80%81%E5%AF%B9%E8%B1%A1%E3%80%81%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E8%BD%AC%E6%8D%A2/" rel="prev" title="js中数组、对象、字符串的转换">
                  <i class="fa fa-chevron-left"></i> js中数组、对象、字符串的转换
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/myblog/2021/01/14/%E9%97%AD%E5%8C%85/" rel="next" title="闭包">
                  闭包 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">匡宇迪</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/myblog/js/utils.js"></script><script src="/myblog/js/motion.js"></script><script src="/myblog/js/next-boot.js"></script>

  
<script src="/myblog/js/local-search.js"></script>






  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>






<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1.7.0/dist/gitalk.css">

<script>
NexT.utils.loadComments('#gitalk-container', () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1.7.0/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : '1926f8a1c453a1121d8d',
      clientSecret: '426722f9eef7599f8ebb3220379e704ea007a5f2',
      repo        : '',
      owner       : '',
      admin       : [''],
      id          : '105656d8f1c66a07595f5c26e0caa3e6',
        language: '',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

</body>
</html>
